package org.visionlibrary.image.geomtric.lines.direction;

import java.util.List;

import org.visionlibrary.image.geomtric.model.Point2d;
import org.visionlibrary.image.geomtric.model.Vector2d;


/**
 * Klasa realizujaca wyznaczanie dominujacego kierunku zbioru odcinkow jako
 * maksymalnej odleglosci miedzy dwoma punktami nalezacymi do zbioru odcinkow. 
 */
public class MaximalDistanceDirection implements LinesDirection {
	@Override
	public double direction(List<Vector2d> lines) {
		Point2d[] points = new Point2d[lines.size() * 2];
		for (int i = 0; i < lines.size(); ++i) {
			points[i] = lines.get(i).getFirst();
			points[i + 1] = lines.get(i).getLast();
		}

		Point2d p1 = points[0];
		Point2d p2 = points[1];
		double maxDistance = Point2d.distance(p1, p2);
		for (int i = 0; i < lines.size() - 1; ++i) {
			for (int j = i + 1; j < lines.size(); ++j) {
				double distance = Point2d.distance(points[i], points[j]);
				if (distance > maxDistance) {
					maxDistance = distance;
					p1 = points[i];
					p2 = points[j];
				}
			}
		}
		
		return Vector2d.angle(p1, p2, false);
	}
}
